// Do not remove the include below
/* specific uC configuration */




//#include <Arduino.h>
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/interrupt.h>
#include <teste.h>




uint8_t rub_index[100];
volatile unsigned long milis = 0;
unsigned long now = 0;

/********** TEST ONLY *******************************/
void teste_init_blink_led_with_delay();
void teste_blink_led_with_delay();
/****************************************************/

void			teste_blink_with_timer	(int ms);

void 			teste_USART_init		();
void 			teste_USART_send		(unsigned char data);
unsigned char 	teste_USART_receive		(void);
void			teste_USART_put_string	(char * ptr_to_String);
void			teste_timer0_init		(void);


int main()
{


	char lub_sent_txt[100] = "Ceau Gigi";
	char * pub_recieved_txt;
	pub_recieved_txt = lub_sent_txt;
/***** CALL OF THE INITIALIZATION FUNCTIONS *******************/
	teste_USART_init();
	teste_init_blink_led_with_delay();
	teste_timer0_init();
	teste_blink_with_timer(1000);
/**************************************************************/




	while(1)
	{
		if(lub_sent_txt != NULL)
		{
			teste_USART_put_string(lub_sent_txt);
			lub_sent_txt[0] = NULL;
		}
		*pub_recieved_txt = NULL;
		*pub_recieved_txt = teste_USART_receive();
		if(*pub_recieved_txt == 1)
		{
			teste_blink_with_timer(100);
		}
		else
		{
			teste_blink_with_timer(1000);
		}
//		while(*pub_recieved_txt != NULL)
//		{
//			*pub_recieved_txt = teste_USART_receive();
//			pub_recieved_txt++;
//			teste_blink_with_timer(100);
//		}
//		teste_blink_with_timer(500);
	}
	return 0;
}

/* initialization of timer 0 to trigger an interrupt every milisecond */
void teste_timer0_init (void)
{
	TCCR0A = 1 << WGM01;					// TIMER is now in CTC mode
	TCCR0B = ( 1 << CS01 ) | ( 1 << CS00 );	// 1:64 prescaler
	OCR0A = 249;							// value to have an compare at
	TIMSK0 = 1 << OCIE0A;					// Enable timer interrupts
	sei();									// Enable global interrupts
}

ISR(TIMER0_COMPA_vect)
{
	milis++;
}

/* will send the string for the given string address */
void teste_USART_put_string	(char * ptr_to_String)
{
	while(*ptr_to_String != 0x00)
	{
		teste_USART_send(*ptr_to_String);
		ptr_to_String++;
	}
}



/* Serial receive data */
unsigned char teste_USART_receive(void)
{
/* In the first line the while loop is used to
 * pool the receive register, and if there is
 * new data in that register we return the data */

	 while(!(UCSR0A & (1<<RXC0)));
	 return UDR0;
}


/* Serial send data */
void teste_USART_send(unsigned char data)
{
/* The first line is a bit odd, but it as its reasons,
 * that line is checking if there is space in the Atmega
 * send buffer(the atmega chip as a small 3 byte hardware
 * buffer in the TX/send channel) to put a new char/byte,
 * if yes the char/byte is loaded into the UDR0 register
 * which makes part of the USART hardware and if something
 * is loaded into that register it will be sent by the USART */

	while(!(UCSR0A & (1<<UDRE0)));
	UDR0 = data;
}



/* Serial port initialization */
void teste_USART_init()
{
	/* UBRR0H and UBRR0L represents the baud-rate */
	UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);
	UBRR0L = (uint8_t)(BAUD_PRESCALLER	 );

	/* UCSR0B is the register for control of Rx(RXEN0) and TX(TXEN0) */
	UCSR0B = (1<RXEN0)|(1<<TXEN0);
	/* UCSZ00 and UCZS01 bits */
	UCSR0C = (1<<UCSZ00)|(1<<UCSZ01);
}








void teste_blink_with_timer(int ms)
{
	if(milis-now >= ms)
	{
		now = milis;
		PORTD ^= (1<<PORTD7);    //Toggle the led at every 500ms/0.5s/2hz
	}
}




/********** TEST ONLY *******************************/
void teste_init_blink_led_with_delay()
{
	/* PIN 7 ARDUINO */
	PORTD = 1 << PORTD7;		// SET PORT B PIN 5 ARDUINO PIN_13 TO HIGH (pull-up)
	DDRD  = 1 << DDD7;			// SET PORT B PIN 5 ARDUINO PIN_13 AS OUTPUT PIN
}

void teste_blink_led_with_delay()
{
	PORTD = (1 << PORTD7);
	_delay_ms(500);
	PORTD = (0 << PORTD7);
	_delay_ms(1000);
}
/****************************************************/
